package io.reader;

import com.sun.tools.javac.util.Pair;
import framework.AbstractTest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import utils.Log;
import utils.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by shane on 2018/5/20.
 */
public class AntApiFile extends AbstractTest {
    protected static final String TAG = AntApiFile.class.getName();

    @Override
    public void test(String[] args) {
        String testFilePath = ANT_API_FILE;

        try {
            read(testFilePath);
            SimpleDateFormat format = new SimpleDateFormat(
                FORMAT_DATE_TIME
            );
            Date date = new Date();
            Log.vn(format.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String getTag() {
        return TAG;
    }

    public static void read(String filename) throws IOException {
        if (StringUtils.isEmpty(filename)) {
            return;
        }

        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line;
        List<Pair<String, String>> lines = new ArrayList<>();

        Pair<String, String> start = null;
        Pair<String, String> end;
        long connectTime = 0;
        while ((line = in.readLine()) != null) {
            Pair<String, String> pair = parseLine(line);
            lines.add(pair);
            if (pair.snd.equals("52")) {
                start = pair;
            } else {
                if (start != null) {
                    end = pair;
                    connectTime += duration(start, end);
                    Log.vn(start);
                    Log.vn(end);
                    start = null;
                }
            }
        }
        in.close();

        Pair<String, String> last = lines.get(lines.size()-1);
        Pair<String, String> first = lines.get(0);
        long total = duration(first, last);
        Log.vn("total:" + total);
        Log.vn("connectTime:" + connectTime);
        double rate = connectTime * 100 / (double)total;
        String res = "rate:" + String.valueOf(rate) + "%";
        Log.vn(res);
    }

    static final String PREFIX = "[I][";
    static final String MODEL = "2020-03-31 +8.0 00:23:47";
    static final String MODEL_DATE = "2020-03-31";
    static final String MODEL_TIME = "00:23:47";
    static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat(FORMAT_DATE_TIME);

    static Pair<String, String> parseLine(String line) {
        String status = line.substring(line.lastIndexOf(":")+1);

        String timeStamp = line.substring(PREFIX.length(), PREFIX.length()+MODEL.length());

        String timeStamp2 = StringUtils.getAppendString(timeStamp.substring(0, MODEL_DATE.length()), " ", timeStamp.substring(timeStamp.length() - MODEL_TIME.length()));
        return new Pair<>(timeStamp2, status);
    }

    static long duration(Pair<String, String> start, Pair<String, String> end) {
        return StringUtils.stringToTimeStamp(end.fst, DATE_TIME_FORMAT) - StringUtils.stringToTimeStamp(start.fst, DATE_TIME_FORMAT);
    }

}
